package com.sir.tao.carhire.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sir.tao.carhire.common.CommonResult;
import com.sir.tao.carhire.entity.table.UserEntity;
import com.sir.tao.carhire.service.UserService;
import com.sir.tao.carhire.util.PageUtil;
import com.sir.tao.carhire.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author dyy
 * @data 2021/12/15 14:54
 * @description:
 */
@Api(tags = "用户管理接口")
@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 分页查询所有的用户
     * @return 用户列表
     */
    @ApiOperation("获取所有用户")
    @ApiImplicitParam(name = "获取所有用户", value = "获取所有用户")
    @GetMapping("/list")
    public CommonResult<?> selectUsers(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                       String search){
        List<UserEntity> list = userService.list(new LambdaQueryWrapper<UserEntity>()
                .like(!StringUtils.isEmpty(search),UserEntity::getUserName,search)
                .or()
                .like(!StringUtils.isEmpty(search),UserEntity::getPhone,search)
        );
        if(Objects.isNull(list)){
            return CommonResult.fail("查询失败");
        }else {
            PageUtil pageUtil = PageUtil.doPage(pageNo,pageSize,list);
            return CommonResult.success(pageUtil);
        }
    }


    /**
     * 新增一个客户信息
     * @param user 客户信息
     * @return 是否成功
     */
    @ApiOperation("新增一个客户信息")
    @ApiImplicitParam(name = "新增一个客户信息", value = "新增一个客户信息")
    @PostMapping("/add")
    public CommonResult<?> addUser(@RequestBody UserEntity user){
        boolean i = userService.save(user);
        if(i){
            return CommonResult.success("添加成功");
        }else {
            return CommonResult.fail("添加失败");
        }
    }

    /**
     * 修改一个客户信息
     * @param user 客户信息
     * @return 是否成功
     */
    @ApiOperation("修改一个用户")
    @ApiImplicitParam(name = "修改一个用户", value = "修改一个用户")
    @PutMapping("/update")
    public CommonResult<?> updateUser(@RequestBody UserEntity user){
        boolean i = userService.updateById(user);
        if(i){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.fail("修改失败");
        }
    }



    /**
     * 游览客户信息
     * @param userId 游览客户信息
     * @return 游览客户信息
     */
    @ApiOperation("游览客户信息")
    @ApiImplicitParam(name = "游览客户信息", value = "游览客户信息")
    @GetMapping("/{userId}")
    public CommonResult<?> selectUserByUserId(@PathVariable String userId){
        UserEntity userEntity = userService.getById(userId);

        if(Objects.isNull(userEntity)){
            return CommonResult.fail("查询失败");
        }else {
            return CommonResult.success(userEntity);
        }
    }

    /**
     * 根据用户id删除用户
     * @param userId 用户id
     * @return 是否成功
     */
    @ApiOperation("根据用户id删除用户")
    @ApiImplicitParam(name = "根据用户id删除用户", value = "根据用户id删除用户  ")
    @DeleteMapping("/delete/{userId}")
    public CommonResult<?> deleteUser(@PathVariable String userId){
        boolean i = userService.removeById(userId);
        if(i){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.fail("删除失败");
        }
    }






}
